
package net.ck.hzq.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;

import java.util.HashMap;
import java.util.List;

import Bean.POSSelectBean;


/**
 * POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class PosFlowedAdapter extends DelegateAdapter.Adapter<PosFlowedAdapter.ProfitVH> {

    private final Context mContext;
    private List<String> data;//数据
    HashMap<String, List<POSSelectBean.DataBean.ItemsBean>> map = new HashMap<>();
    public int index = -1;
    PosRecord2Adapter posRecordAdapter;


    public PosFlowedAdapter(Context context, List<String> mdata, HashMap<String, List<POSSelectBean.DataBean.ItemsBean>> mmap) {
        mContext = context;
        data = mdata;
        map = mmap;
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public ProfitVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ProfitVH(LayoutInflater.from(mContext).inflate(R.layout.layout_flowed_device, viewGroup, false));
    }


    @Override
    public void onBindViewHolder(@NonNull ProfitVH holder, int position) {
        if (position < 0 || position >= data.size()) {
            return;
        }
        String name = map.get(data.get(position)).get(0).getFlowCustomName();
        holder.custom.setText(name);
        holder.flow_custom_head.setOnClickListener(v -> {
            holder.custom.setSelected(!holder.custom.isSelected());
            holder.customArrow.setSelected(!holder.customArrow.isSelected());
            holder.deviceList.setVisibility(holder.customArrow.isSelected() ? View.VISIBLE : View.GONE);
        });

        posRecordAdapter = new PosRecord2Adapter(mContext, map.get(data.get(position)));
        holder.deviceList.setLayoutManager(new LinearLayoutManager(holder.deviceList.getContext())); //设置布局管理器
        holder.deviceList.setNestedScrollingEnabled(false);//禁止滑动
        holder.deviceList.setAdapter(posRecordAdapter);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ProfitVH extends RecyclerView.ViewHolder {
        TextView custom;
        ImageView customArrow;
        RecyclerView deviceList;
        RelativeLayout flow_custom_head;
        LinearLayout handler_info;


        ProfitVH(@NonNull View itemView) {
            super(itemView);

            custom = itemView.findViewById(R.id.flow_custom_name);
            customArrow = itemView.findViewById(R.id.flow_expand_arrow);
            handler_info = itemView.findViewById(R.id.handler_info);
            deviceList = itemView.findViewById(R.id.device_list);
            flow_custom_head = itemView.findViewById(R.id.flow_custom_head);

        }
    }
}
